﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yunchang;

namespace AutoChess
{
    public class HeroIdelState : Yunchang.FsmStateBase<HeroBehaviourState>
    {
        HeroBehaviourCtr ctr ;
        private float timmer;
        private float endTime;
        private bool needDisplayAnim;
        private bool needPlayEnterAnim = false;

        public HeroIdelState(HeroBehaviourState stateType, HeroBehaviourCtr controller) : base(stateType, controller)
        {
            ctr = controller;
        }
        public override void Enter()
        {
            needDisplayAnim = ctr.heroRenderer != null && ctr.heroRenderer.heroUnit != null && ctr.heroRenderer.heroUnit.MapGrid != null && ctr.heroRenderer.heroUnit.MapGrid.GridType != GridType.ShopGrid;
            timmer = 0f;
            endTime = Table.DataModel.TableConfig.autoChessShowStateTime;
            //ctr.heroRenderer.PlayAnimation(AniName.STAND);
            needPlayEnterAnim = true;
        }

        public override void Update()
        {
            if (needPlayEnterAnim)
            {
                if (ctr.heroRenderer == null || !ctr.heroRenderer.IsCanPlayAnim())
                    return;
                needPlayEnterAnim = false;
                ctr.heroRenderer.PlayAnimation(AniName.STAND);
            }
            else
            {
                if (!needDisplayAnim)
                    return;

                timmer += Time.unscaledDeltaTime;
                if (timmer > endTime)
                {
                    HeroEnterShowState();
                    timmer = 0;
                }
            }
        }

        private void HeroEnterShowState()
        {
            if (ctr.heroRenderer == null || ctr.heroRenderer.heroUnit == null)
                return;
            var tableModel = Table.Blo.TableSailorModelBlo.GetTableSailorModelByHeroIdAndAppearence(ctr.heroRenderer.heroUnit.HeroID);
            var animes = Table.Blo.TableHeroSceneActionBlo.GetSceneActions(tableModel.ModelId);
            if (animes == null || animes.Count == 0)
                return;
            var anim = animes[Random.Range(0, animes.Count - 1)];
            ctr.showData = new ShowPackage(anim.animName, null);
            ctr.SwitchState(HeroBehaviourState.Show);
        }
    }
}
